$(function() {

  
    $.each(categories,function(index,value){
      $(".parent").append("<option value="+value.id+">"+value.name+"</option>");
    });

    $('.parent').on('change', function() {
      var that = $(this);
      var pid = that.val();
      $.each(categories,function(index,value){
        if( value.id == pid){
          var children = value.children;
          $(".child").empty()
          $.each(children,function(index,child){
            $(".child").append("<option value="+child.id+">"+child.name+"</option>");
          });
        }  　　　 
      });
    });


    $('.child').on('change', function() {
      var that = $(this);
      var category_id = that.val();
    });


    //global variable of checker
    var checker = null;
    //global variable of staff
    var staff = null;

    /**
     * submit for create shop
     */
    $('.submit').on('click', function(e) {
    

      $.post('/merchant/shop', {
        '_token':$("input[name='_token']").attr('value'),
        'branch_id':branch,
        'type': type,
        'trade': trade,
        'category_id': category,
        'member_id': checker,
        'staff_id': staff,
        'progress': progress,
        'address':$("input[name='address']").val(),
        'opening':$("input[name='opening']").val(),      
        'lon': $("input[name='lon']").val(),      
        'lat': $("input[name='lat']").val(),
        'is_submit': true
      }, function(data) {
        console.log(data);        
        layer.msg(data.message, { time:2000,shift: -1 },function(){
          if(data.status === true ){
            parent.location.reload();
            parent.layer.close(parent.layer.index);
          }
        });
        
      },'json').error(function(data){
        layer.msg(data.responseJSON.message);
      });
          
    });


    $("input:checkbox[name='type']").on('click', function (e) {
        var that = $(this);
        var target = that.data('target');
        $("input:checkbox[name='type']").not(this).attr("checked", false);

        count = $("input:checkbox[name='type']:checked").length;
        if (count <= 1) {
            that.prop("checked", true);
        }
        if( that.is(':checked') ) {
          $(".type").hide();
          $("."+target).show();
        }
        else{
          $(".type").hide();
        }
    });

    $("input:checkbox[name='effect']").on('click', function (e) {
        var that = $(this);
        var target = that.data('target');
        $("input:checkbox[name='effect']").not(this).attr("checked", false);

        count = $("input:checkbox[name='effect']:checked").length;
        if (count <= 1) {
            that.prop("checked", true);
        }
        if( that.is(':checked') ) {
          $(".effect").hide();
          $("."+target).show();
        }
        else{
          $(".effect").hide();
        }
    });

    /**
     * single select for checkbox
     */
    $("input:checkbox[name='scope']").on('click', function (e) {
        var that = $(this);
        $("input:checkbox[name='scope']").not(this).attr("checked", false);
        count = $("input:checkbox[name='scope']:checked").length;
        if (count <= 1) {
            that.prop("checked", true);
        }
    });


    /**
     * Autocomplete the staff's infomation   
     */
    $("input[name='staff']").autocomplete({
      source : function(request, response) {
        $.get('/util/charge', {
            'term':request.term,
          }, function(data) {
            response($.map(data,function(item) {
              return { 
                label: item.name+' - '+item.username, 
                label: item.name+' - '+item.username, 
                id:item.id
              }
            }));
            
          },'json');
        },

      select:function(e, ui) {
        staff = ui.item.id;
      },
      minLength: 2,
    });

    $('#date_range').daterangepicker({
        autoApply: true,
        //showDropdowns: true,
        ranges: {
            '本周': [moment().startOf('week'), moment().endOf('week')],
            '本月': [moment().startOf('month'), moment().endOf('month')],
            '本年度': [moment().startOf('year'), moment().endOf('year')]
        },
        opens: 'right',
        showWeekNumbers: true,
        locale: {
            format: 'YYYY-MM-DD',
            separator: " - ",
            applyLabel: '确定',
            cancelLabel: '取消',
            customRangeLabel: '自定义',
            firstDay: 1
        },
        startDate: moment(),
        endDate: moment(),
        }, function(start, end, label) {
            $('input[name="date_begin"]').attr('value',start.format('YYYY-MM-DD'));
            $('input[name="date_end"]').attr('value',end.format('YYYY-MM-DD'));
        });

    /**
     * Cancel button event
     */
    $('.cancel').on('click', function() {
      parent.layer.closeAll();
    });

});